/**
   LoginFilter
 * Time:2018年5月27日
 */
package com.ssm.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;



/**
 * @author 15199
 *
 */
public class LoginFilter implements Filter  {

	
	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
	}
	@Override
	@SuppressWarnings("all")
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		// TODO Auto-generated method stub
		HttpServletRequest servletRequest=(HttpServletRequest) request;
		HttpServletResponse servletResponse=(HttpServletResponse) response;   
		String URI=servletRequest.getRequestURI();
		int account= (int) servletRequest.getSession().getAttribute("account");
		int password= (int) servletRequest.getSession().getAttribute("password");
		if(account+""==null||password+""==null){
			String contextPath=servletRequest.getContextPath();
			String requestURI=servletRequest.getRequestURI();
			String path=requestURI.substring(contextPath.length());
			if(path.equals("/")){
				chain.doFilter(request, response);
			}else{
				servletResponse.sendRedirect("/");
			}
		}else{
			chain.doFilter(request, response);
		}
	}
	@Override
	public void destroy() {
	}
}
